/**
* 比赛投票榜页面
* path:'/game/gamevote' ,query:{gameId:'必传',gameStep:'必传',gameName:'必传',identity:'必传'}
* gameId:比赛ID;   gameName：比赛名；
* gameStep:比赛状态；'0':未开始比赛，'1':报名中, '2':报名结束, '3':比赛中, '4':比赛结束， '5':比赛无效
* identity:是否该比赛参塞者   identity = Constant.game.identity.ONLOOKER:旁观者， identity = Constant.game.identity.PLAYER :参数者
*/
<template>
  <div class="game-vote">
    <header class="common-app-header">
      <div class="common-app-btn-back" @click.stop.prevent="back()">
        <i class="iconfont">&#xe60c;</i>
      </div>
      <div class="common-app-title">
        <div class="title">{{gameName}}</div>
      </div>
      <div class="common-app-btn-search" v-show="!isSearch" @click.stop.prevent="search()">
        <i class="iconfont">&#xe60d;</i>
      </div>
      <div class="common-app-btn-share" v-show="!isSearch" @click.stop.prevent="share()">
        <i class="iconfont">&#xe60e;</i>
      </div>
    </header>
    <rank v-if="gameStep<4" v-show="!isSearch" :gameName="gameName" :gameStep="gameStep" :gameId="gameId" :userCode="userCode" :identity="identity" :gameSubKind= 'gameSubKind' ></rank>
    <result v-if="gameStep == 4" v-show="!isSearch"  :gameStep="gameStep" :gameId="gameId" :userCode="userCode"></result>
    <search v-if="renderSearch" v-show="isSearch" :gameName="gameName" :gameStep="gameStep" :gameId="gameId" :userCode="userCode" :identity="identity"></search>
    <game-not-start-modal></game-not-start-modal>
    <subscribe-modal></subscribe-modal>
  </div>
</template>

<script>
  import Constant from 'common/constant'
  import LocalDAO from 'common/localDAO'
  import RemoteDAO from 'common/remoteDAO'
  import Rank from 'components/game/vote/rank'
  import Result from 'components/game/vote/result'
  import Search from 'components/game/vote/search'
  import HeaderTop from 'components/HeaderTop'
  import GameNotStartModal from 'components/modal/GameNotStartModal'
  import SubscribeModal from 'components/modal/SubscribeModal'
  import $ from 'jquery'

  export default {
    name: 'GameVote',
    data () {
      return {
        gameId: this.$route.query.gameId || '',
        gameStep: this.$route.query.gameStep || '',
        gameName: this.$route.query.gameName || '',
        gameSubKind: this.$route.query.gameSubKind || '',
        identity: this.$route.query.identity || Constant.game.identity.ONLOOKER,
        renderSearch:false,
        isSearch: false,
        userCode: LocalDAO.getUserCode()
      }
    },
    components: {
      'rank': Rank,
      'result': Result,
      'search': Search,
      'game-not-start-modal': GameNotStartModal,
      'subscribe-modal': SubscribeModal
    },
    methods: {
      back(){
        if (this.isSearch) {
          this.isSearch = false;
        } else if (typeof (this.$route.query.cleanStorage) !== 'undefined') {
          window.location.href = "gtjanormal://goback";
        } else {
          this.$router.go(-1);
        }
      },
      search(){
        if(!this.renderSearch){
          this.renderSearch = true;
        }
        this.isSearch = true;
      },
      share(){
        var self = this
        var players = LocalDAO.getGameVote().players || [];
        var data = {
          players: players,
          gameStep: self.gameStep
        };
        data = JSON.stringify(data);

        $.ajax({
          url: "https://dl.app.gtja.com/yyz/data/api/niurenshare/share",
          type: "POST",
          dataType: "json",
          contentType: 'application/json;charset=UTF-8',
          data: JSON.stringify({usercode: self.userCode, type: "gamevote", content: data}),
          success: function (response) {
            console.log(JSON.stringify(response));
            // window.location.href = "share.gamevote.html?url="+response.url+"&userCode="+self.userCode; //直接跳转，测试用
            var title = "帮我成为人气王";
            var content = "就差你的临门一脚啦~";
            var linkurl = Constant.url.CDN + "share.gamevote.html?url=" + response.url + "&userCode=" + self.userCode;
            var moments = "all";
            var icon = "https://dl.app.gtja.com/nrng/shareimages/dianzanwoderen.png";
            RemoteDAO.share(title, content, linkurl, moments, icon);
          },
          error: function (e) {
            alert("error");
          }
        });
      }
    }
  }
</script>

<style scoped>

</style>
